package com.ccloud.common.mybatisplus.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.ccloud.common.mybatisplus.intercepters.AutoCreateIdInterceptor;
import com.ccloud.common.mybatisplus.properties.DruidProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Author chentl
 * @Version V1.0
 * @Project wh-ccloud-frame.
 * @Title MybaitsPlusAutoConfiguration.java
 * @Description Mybatis-Plus自动配置类
 * @Package com.ccloud.common.mybatisplus.config
 * @Date 2021/5/14 14:05
 * @Copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
@Configuration
@MapperScan(basePackages = "com.ccloud.**.mapper")
@ConditionalOnProperty(prefix = "spring.datasource.druid", name = "url")
public class MybaitsPlusAutoConfiguration {

    @Resource
    private DruidProperties druidProperties;

    /**
     * druid配置
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DruidProperties druidProperties() {
        return new DruidProperties();
    }


    /**
     * druid数据库连接池
     */
    @Bean(initMethod = "init")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    /**
     * mybatis 乐观锁
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * @Author chentl
     * @Description 自动生成ID拦截器
     * @Date 14:16 2021/5/14
     * @Param []
     * @Return com.ccloud.common.mybatisplus.intercepters.AutoCreateIdInterceptor
     **/
    @Bean
    public AutoCreateIdInterceptor autoCreateIdInterceptor(){
        return new AutoCreateIdInterceptor();
    }
}
